#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include <cstdio>
#include <vector>
#include <mutex>
#include <memory>
#include "LockGuard.hpp"

int g_ticket_num = 10000;
int g_cnt = 0;
pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;

void *func(void *args)
{
    while (g_ticket_num > 0)
    {
        LockGuard lockguard(&mtx);
        if (g_ticket_num > 0)
        {
            std::cout << g_ticket_num << "票" << std::endl;
            g_ticket_num--;
        }
    }
    return nullptr;
}
int main()
{

    std::vector<pthread_t> tids(10);

    for (int i = 0; i < 10; i ++ )
    {
        pthread_t tid;
        char *name = new char[128];
        sprintf(name, "pthread -%d", i);
        pthread_create(&tid, nullptr, func, (void *)name);
        tids.emplace_back(tid);
    }
    for (auto x : tids)
        pthread_join(x, nullptr);

    sleep(100);
    return 0;
}
